package com.qzimp.fujiancuo.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.CacheableServiceImpl;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qzimp.fujiancuo.entity.Admin;
import com.qzimp.fujiancuo.entity.AdminLog;
import com.qzimp.fujiancuo.entity.vo.AdminHaveLogsVo;
import com.qzimp.fujiancuo.entity.vo.ResultCode;
import com.qzimp.fujiancuo.exceptions.ApiException;
import com.qzimp.fujiancuo.mapper.AdminMapper;
import com.qzimp.fujiancuo.service.AdminLogService;
import com.qzimp.fujiancuo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 *  服务层实现。
 *
 * @author 吴宗波
 * @since 2024-11-21
 */
@Service
@CacheConfig(cacheNames = "admin")
public class AdminServiceImpl extends CacheableServiceImpl<AdminMapper, Admin> implements AdminService {
    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private AdminLogService adminLogService;
    @Cacheable(key = "#root.methodName + ':' + #adminName+':'+#password")
    @Override
    public Admin login(String adminName, String password) {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("admin_name",adminName);
        queryWrapper.eq("password",password);
        return adminMapper.selectOneByQuery(queryWrapper);
    }
    @Cacheable(key = "#adminName")
    @Override
    public AdminHaveLogsVo queryAdminHaveLogs(String adminName) {
        //获取管理员信息通过管理员账号

        Admin admin=this.queryAdminByName(adminName);

        if(admin==null){
            throw  new ApiException(ResultCode.CUSTOM_ERROR,"管理员账户不存在");
        }

        // 通过管理员编号获取日志信息
        List<AdminLog> logList= adminLogService.queryAdminLogs(admin.getId());


        //把所有的信息放到包装类AdminHaveLogsVo
        AdminHaveLogsVo adminHaveLogsVo=new AdminHaveLogsVo();

        adminHaveLogsVo.setAdminName(adminName);
        adminHaveLogsVo.setAdminLogList(logList);

        return adminHaveLogsVo;
    }
    @Cacheable(key = "#adminName")
    @Override
    public Admin queryAdminByName(String adminName) {

        QueryWrapper queryWrapper=new QueryWrapper();

        queryWrapper.eq("admin_name",adminName);

        return adminMapper.selectOneByQuery(queryWrapper);
    }


}